package com.dandelion.api.controller;

import com.rockit.core.code.ErrorCode;
import com.rockit.core.exception.ApiException;
import com.rockit.core.exception.IllegalApiParameterException;
import com.rockit.core.exception.TargetObjectNotExistException;
import com.rockit.core.pojo.JsonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.convert.ConversionFailedException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

@RestController
@RequestMapping("/address")
public class BaseRestController {
    private static final Logger logger = LoggerFactory.getLogger(BaseRestController.class);

    @ExceptionHandler(Exception.class)
    public JsonResult handleException(Exception e) {
        int errorCode;
        String msg;
        if (e instanceof IllegalArgumentException || e instanceof IllegalApiParameterException) {
            errorCode = ErrorCode.ILLEGAL_PARAMETER;
            msg = e.getMessage();
        } else if (e instanceof MissingServletRequestParameterException) {
            errorCode = ErrorCode.ILLEGAL_PARAMETER;
            MissingServletRequestParameterException ex = ((MissingServletRequestParameterException) e);
            msg = "参数" + ex.getParameterName() + "缺失";
        } else if (e instanceof MethodArgumentTypeMismatchException || e instanceof ConversionFailedException) {
            MethodArgumentTypeMismatchException ex = ((MethodArgumentTypeMismatchException) e);
            errorCode = ErrorCode.ILLEGAL_PARAMETER;
            msg = "参数'" + ex.getName() + "'数据类型不正确";
        } else if (e instanceof TargetObjectNotExistException) {
            TargetObjectNotExistException ex = ((TargetObjectNotExistException) e);
            errorCode = ex.getErrorCode();
            msg = "对象'" + ex.getTargetName() + "'不存在";
        } else if (e instanceof ApiException) {
            errorCode = ((ApiException) e).getErrorCode();
            msg = "System Error, ErrorCode:" + errorCode;
        } else {
            logger.error("System Error!!!", e);
            errorCode = ErrorCode.SYS_ERR;
            msg = "System Error";
        }

        return JsonResult.error(errorCode, msg);
    }

    static void assertValid(BindingResult result) {
        FieldError fieldError = result.getFieldError();
        if (fieldError != null) {
            throw new IllegalApiParameterException(fieldError.getDefaultMessage());
        }
    }

    public static JsonResult ok(Object object) {
        return JsonResult.ok(object);
    }

    public static JsonResult ok() {
        return JsonResult.ok();
    }
}
